/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wwl;

import com.wwl.arrayqueue.ArrayQueue;
import com.wwl.arrayqueue.Queue;
import com.wwl.linkedqueue.LinkedListQueue;
import com.wwl.loopqueue.LoopQueue;
import org.junit.Test;

import java.util.Random;

public class VsQueue {

    private final Integer opCount = 1000000;

    @Test
    public void vs() {
        vs1();
        vs2();
        vs3();
    }

    private void vs3() {
        long startTime = System.nanoTime();
        LinkedListQueue<Integer> queue = new LinkedListQueue<>();
        testQueue(queue, opCount);
        System.out.println("LinkedListQueue:" + (System.nanoTime() - startTime));
    }

    private void vs1() {
        long startTime = System.nanoTime();
        ArrayQueue<Integer> queue = new ArrayQueue<>();
        testQueue(queue, opCount);
        System.out.println("ArrayQueue:" + (System.nanoTime() - startTime));
    }

    private void vs2() {
        long startTime = System.nanoTime();
        LoopQueue<Integer> queue = new LoopQueue<>();
        testQueue(queue, opCount);
        System.out.println("LoopQueue:" + (System.nanoTime() - startTime));
    }

    private void testQueue(Queue<Integer> queue, Integer opCount) {
        for (int i = 0; i < opCount; i++) {
            queue.enqueue(new Random().nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0; i < opCount; i++) {
            queue.dequeue();
        }
    }
}
